<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>发布文章 - TechBlog</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <!-- 移除固定的GitHub主题，改为动态加载 -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/marked/9.1.2/marked.min.js"></script>
    <style>
        :root {
            /* 亮色主题变量 */
            --bg-color: #ffffff;
            --text-color: #24292f;
            --border-color: #d0d7de;
            --card-bg: #f6f8fa;
            --header-bg: #f6f8fa;
            --accent-color: #0969da;
            --accent-hover: #0550ae;
            --code-bg: rgba(175, 184, 193, 0.2);
            --shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
            --tag-bg: #ddf4ff;
            --tag-color: #0969da;
            --filter-bg: #f6f8fa;
            --input-bg: #ffffff;
            --success-color: #28a745;
            --warning-color: #ffc107;
            --error-color: #d73a49;
        }

        .dark-theme {
            /* 深色主题变量 */
            --bg-color: #0d1117;
            --text-color: #e6edf3;
            --border-color: #30363d;
            --card-bg: #161b22;
            --header-bg: #161b22;
            --accent-color: #2f81f7;
            --accent-hover: #1c6bdb;
            --code-bg: rgba(110, 118, 129, 0.4);
            --shadow: 0 1px 3px rgba(0, 0, 0, 0.5), 0 1px 2px rgba(0, 0, 0, 0.7);
            --tag-bg: #388bfd26;
            --tag-color: #58a6ff;
            --filter-bg: #161b22;
            --input-bg: #0d1117;
            --success-color: #3fb950;
            --warning-color: #d29922;
            --error-color: #f85149;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            transition: background-color 0.3s, color 0.3s, border-color 0.3s;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
            background-color: var(--bg-color);
            color: var(--text-color);
            line-height: 1.5;
        }

        .container {
            max-width: 1280px;
            margin: 0 auto;
            padding: 0 20px;
        }

        /* 头部导航 */
        header {
            background-color: var(--header-bg);
            border-bottom: 1px solid var(--border-color);
            position: sticky;
            top: 0;
            z-index: 100;
        }

        .navbar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 16px 0;
            position: relative;
        }

        .logo {
            display: flex;
            align-items: center;
            gap: 10px;
            font-weight: 600;
            font-size: 20px;
            color: var(--text-color);
            text-decoration: none;
            z-index: 101;
        }

        .logo i {
            color: var(--accent-color);
        }

        .nav-links {
            display: flex;
            gap: 24px;
        }

        .nav-links a {
            color: var(--text-color);
            text-decoration: none;
            font-size: 14px;
            font-weight: 500;
            transition: all 0.2s ease;
        }

        .nav-links a:hover {
            color: var(--accent-color);
        }

        .nav-links a.active {
            color: var(--accent-color);
            font-weight: 600;
        }

        .nav-actions {
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .theme-toggle {
            background: none;
            border: 1px solid var(--border-color);
            border-radius: 6px;
            padding: 8px 12px;
            color: var(--text-color);
            cursor: pointer;
            display: flex;
            align-items: center;
            gap: 6px;
            font-size: 14px;
            transition: all 0.2s ease;
        }

        .theme-toggle:hover {
            background-color: var(--code-bg);
        }

        .mobile-menu {
            display: none;
            background: none;
            border: 1px solid var(--border-color);
            border-radius: 6px;
            color: var(--text-color);
            cursor: pointer;
            padding: 8px 12px;
            font-size: 16px;
            transition: all 0.2s ease;
        }

        .mobile-menu:hover {
            background-color: var(--code-bg);
        }

        /* 主要内容区域 */
        .main-content {
            padding: 32px 0;
        }

        /* 标题区域 */
        .section-header {
            margin-bottom: 24px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 16px;
        }

        .section-header h2 {
            font-size: 24px;
            font-weight: 600;
            margin-bottom: 0;
        }

        /* 编辑器容器 */
        .editor-container {
            display: flex;
            flex-direction: column;
            gap: 24px;
        }

        /* 文章元数据表单 */
        .article-meta {
            background-color: var(--card-bg);
            border: 1px solid var(--border-color);
            border-radius: 6px;
            padding: 24px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group:last-child {
            margin-bottom: 0;
        }

        .form-label {
            display: block;
            margin-bottom: 8px;
            font-size: 14px;
            font-weight: 600;
        }

        .form-input {
            width: 100%;
            padding: 12px;
            border: 1px solid var(--border-color);
            border-radius: 6px;
            background-color: var(--input-bg);
            color: var(--text-color);
            font-size: 14px;
            transition: border-color 0.2s ease;
        }

        .form-input:focus {
            outline: none;
            border-color: var(--accent-color);
            box-shadow: 0 0 0 3px rgba(9, 105, 218, 0.1);
        }

        .dark-theme .form-input:focus {
            box-shadow: 0 0 0 3px rgba(47, 129, 247, 0.2);
        }

        .form-textarea {
            min-height: 120px;
            resize: vertical;
        }

        .form-select {
            appearance: none;
            background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23666' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
            background-repeat: no-repeat;
            background-position: right 12px center;
            background-size: 16px;
            padding-right: 40px;
        }

        .tags-input-container {
            display: flex;
            flex-wrap: wrap;
            gap: 8px;
            padding: 8px;
            border: 1px solid var(--border-color);
            border-radius: 6px;
            background-color: var(--input-bg);
            min-height: 46px;
            align-items: center;
        }

        .tag {
            display: inline-flex;
            align-items: center;
            background-color: var(--tag-bg);
            color: var(--tag-color);
            padding: 4px 8px;
            border-radius: 6px;
            font-size: 12px;
            font-weight: 500;
        }

        .tag-remove {
            margin-left: 4px;
            cursor: pointer;
            opacity: 0.7;
        }

        .tag-remove:hover {
            opacity: 1;
        }

        .tags-input {
            flex: 1;
            border: none;
            outline: none;
            background: transparent;
            color: var(--text-color);
            font-size: 14px;
            min-width: 100px;
        }

        /* 编辑器布局 */
        .editor-layout {
            display: flex;
            gap: 24px;
            height: 600px;
        }

        @media (max-width: 1024px) {
            .editor-layout {
                flex-direction: column;
                height: auto;
            }
        }

        /* 编辑器区域 */
        .editor-section {
            flex: 1;
            display: flex;
            flex-direction: column;
            background-color: var(--card-bg);
            border: 1px solid var(--border-color);
            border-radius: 6px;
            overflow: hidden;
        }

        .editor-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 16px 20px;
            border-bottom: 1px solid var(--border-color);
            background-color: var(--header-bg);
        }

        .editor-title {
            font-size: 16px;
            font-weight: 600;
        }

        .editor-tools {
            display: flex;
            gap: 12px;
        }

        .editor-tool {
            background: none;
            border: 1px solid var(--border-color);
            border-radius: 4px;
            padding: 6px 10px;
            color: var(--text-color);
            cursor: pointer;
            font-size: 12px;
            transition: all 0.2s ease;
        }

        .editor-tool:hover {
            background-color: var(--code-bg);
        }

        .editor-content {
            flex: 1;
            padding: 0;
            overflow: auto;
        }

        .editor-textarea {
            width: 100%;
            height: 100%;
            min-height: 400px;
            border: none;
            outline: none;
            resize: none;
            padding: 20px;
            background-color: var(--input-bg);
            color: var(--text-color);
            font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
            font-size: 14px;
            line-height: 1.6;
        }

        .preview-content {
            padding: 20px;
            overflow: auto;
        }

        .preview-content h1,
        .preview-content h2,
        .preview-content h3,
        .preview-content h4,
        .preview-content h5,
        .preview-content h6 {
            margin-top: 24px;
            margin-bottom: 16px;
            font-weight: 600;
            line-height: 1.25;
        }

        .preview-content h1 {
            font-size: 2em;
            border-bottom: 1px solid var(--border-color);
            padding-bottom: 0.3em;
        }

        .preview-content h2 {
            font-size: 1.5em;
            border-bottom: 1px solid var(--border-color);
            padding-bottom: 0.3em;
        }

        .preview-content h3 {
            font-size: 1.25em;
        }

        .preview-content p {
            margin-bottom: 16px;
        }

        .preview-content ul,
        .preview-content ol {
            margin-bottom: 16px;
            padding-left: 2em;
        }

        .preview-content li {
            margin-bottom: 4px;
        }

        .preview-content blockquote {
            margin: 16px 0;
            padding: 0 1em;
            color: var(--text-color);
            opacity: 0.8;
            border-left: 4px solid var(--border-color);
        }

        .preview-content pre {
            background-color: var(--code-bg);
            border-radius: 6px;
            padding: 16px;
            overflow-x: auto;
            margin: 16px 0;
            font-size: 14px;
            border: 1px solid var(--border-color);
        }

        .preview-content code {
            font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
            padding: 2px 4px;
            background-color: var(--code-bg);
            border-radius: 3px;
            font-size: 0.9em;
        }

        .preview-content pre code {
            padding: 0;
            background: none;
            border-radius: 0;
        }

        .preview-content table {
            border-collapse: collapse;
            width: 100%;
            margin: 16px 0;
        }

        .preview-content table th,
        .preview-content table td {
            padding: 6px 13px;
            border: 1px solid var(--border-color);
        }

        .preview-content table th {
            background-color: var(--code-bg);
            font-weight: 600;
        }

        .preview-content img {
            max-width: 100%;
            height: auto;
        }

        .preview-content a {
            color: var(--accent-color);
            text-decoration: none;
        }

        .preview-content a:hover {
            text-decoration: underline;
        }

        /* 编辑器底部操作栏 */
        .editor-actions {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 16px 20px;
            border-top: 1px solid var(--border-color);
            background-color: var(--header-bg);
        }

        .status-info {
            display: flex;
            align-items: center;
            gap: 12px;
            font-size: 14px;
        }

        .status-dot {
            width: 8px;
            height: 8px;
            border-radius: 50%;
            background-color: var(--warning-color);
        }

        .status-dot.saved {
            background-color: var(--success-color);
        }

        .action-buttons {
            display: flex;
            gap: 12px;
        }

        .btn {
            padding: 8px 16px;
            border-radius: 6px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.2s ease;
            border: 1px solid var(--border-color);
        }

        .btn-primary {
            background-color: var(--accent-color);
            color: white;
            border-color: var(--accent-color);
        }

        .btn-primary:hover {
            background-color: var(--accent-hover);
            border-color: var(--accent-hover);
        }

        .btn-secondary {
            background-color: transparent;
            color: var(--text-color);
        }

        .btn-secondary:hover {
            background-color: var(--code-bg);
        }

        /* 页脚 */
        footer {
            border-top: 1px solid var(--border-color);
            padding: 32px 0;
            text-align: center;
            font-size: 14px;
            color: var(--text-color);
            opacity: 0.8;
        }

        /* 响应式设计优化 */
        @media (max-width: 768px) {
            .container {
                padding: 0 16px;
            }

            .navbar {
                padding: 12px 0;
            }

            .logo {
                font-size: 18px;
            }

            .nav-links {
                position: absolute;
                top: 100%;
                left: 0;
                width: 100%;
                background-color: var(--header-bg);
                border-top: 1px solid var(--border-color);
                flex-direction: column;
                padding: 16px 20px;
                display: none;
                box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            }

            .nav-links.active {
                display: flex;
            }

            .nav-links a {
                padding: 12px 0;
                border-bottom: 1px solid var(--border-color);
                font-size: 16px;
            }

            .nav-links a:last-child {
                border-bottom: none;
            }

            .mobile-menu {
                display: block;
                min-height: 44px;
                min-width: 44px;
                display: flex;
                align-items: center;
                justify-content: center;
            }

            .theme-toggle,
            .mobile-menu {
                padding: 10px 14px;
            }

            .main-content {
                padding: 24px 0;
            }

            .section-header {
                flex-direction: column;
                align-items: flex-start;
            }

            .section-header h2 {
                font-size: 20px;
            }

            .article-meta {
                padding: 20px;
            }

            .editor-header,
            .editor-actions {
                padding: 12px 16px;
            }

            .editor-tools {
                flex-wrap: wrap;
            }

            .action-buttons {
                flex-wrap: wrap;
                justify-content: flex-end;
            }

            .status-info {
                font-size: 12px;
            }

            /* 减少移动端的动画效果 */
            * {
                transition: none;
            }

            /* 保留必要的过渡 */
            .theme-toggle,
            .mobile-menu,
            .nav-links a,
            .btn,
            .editor-tool {
                transition: all 0.2s ease;
            }
        }

        @media (max-width: 480px) {
            .container {
                padding: 0 12px;
            }

            .editor-actions {
                flex-direction: column;
                gap: 12px;
                align-items: stretch;
            }

            .status-info {
                justify-content: center;
            }

            .action-buttons {
                justify-content: center;
            }
        }
    </style>
</head>

<body class="light-theme">
    <header>
        <div class="container">
            <nav class="navbar">
                <a href="index.html" class="logo">
                    <i class="fas fa-code"></i>
                    TechBlog
                </a>

                <div class="nav-links">
                    <a href="index.html">首页</a>
                    <a href="article-list.html">文章</a>
                    <a href="tutorial-list.html">教程</a>
                    <a href="projects.html">项目</a>
                    <a href="about.html">关于</a>
                </div>

                <div class="nav-actions">
                    <button class="theme-toggle" id="themeToggle">
                        <i class="fas fa-moon"></i>
                        <span>深色模式</span>
                    </button>
                    <button class="mobile-menu" id="mobileMenu">
                        <i class="fas fa-bars"></i>
                    </button>
                </div>
            </nav>
        </div>
    </header>

    <div class="container">
        <div class="main-content">
            <div class="section-header">
                <h2>发布新文章</h2>
            </div>

            <div class="editor-container">
                <!-- 文章元数据表单 -->
                <section class="article-meta">
                    <div class="form-group">
                        <label for="articleTitle" class="form-label">文章标题</label>
                        <input type="text" id="articleTitle" class="form-input" placeholder="输入文章标题...">
                    </div>

                    <div class="form-group">
                        <label for="articleExcerpt" class="form-label">文章摘要</label>
                        <textarea id="articleExcerpt" class="form-input form-textarea"
                            placeholder="输入文章摘要..."></textarea>
                    </div>

                    <div class="form-group">
                        <label for="articleCategory" class="form-label">分类</label>
                        <select id="articleCategory" class="form-input form-select">
                            <option value="">选择分类</option>
                            <option value="JavaScript">JavaScript</option>
                            <option value="React">React</option>
                            <option value="Vue">Vue</option>
                            <option value="Node.js">Node.js</option>
                            <option value="CSS">CSS</option>
                            <option value="性能优化">性能优化</option>
                            <option value="算法">算法</option>
                            <option value="数据库">数据库</option>
                        </select>
                    </div>

                    <div class="form-group">
                        <label class="form-label">标签</label>
                        <div class="tags-input-container" id="tagsContainer">
                            <input type="text" class="tags-input" id="tagInput" placeholder="输入标签后按回车...">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="articleCover" class="form-label">封面图片URL</label>
                        <input type="text" id="articleCover" class="form-input" placeholder="输入封面图片URL...">
                    </div>
                </section>

                <!-- 编辑器布局 -->
                <div class="editor-layout">
                    <!-- 编辑器 -->
                    <section class="editor-section">
                        <div class="editor-header">
                            <div class="editor-title">Markdown编辑器</div>
                            <div class="editor-tools">
                                <button class="editor-tool" data-insert="# ">H1</button>
                                <button class="editor-tool" data-insert="## ">H2</button>
                                <button class="editor-tool" data-insert="**粗体**">粗体</button>
                                <button class="editor-tool" data-insert="*斜体*">斜体</button>
                                <button class="editor-tool" data-insert="[链接](url)">链接</button>
                                <button class="editor-tool" data-insert="![图片](url)">图片</button>
                                <button class="editor-tool" data-insert="```\n代码\n```">代码</button>
                            </div>
                        </div>
                        <div class="editor-content">
                            <textarea id="markdownEditor" class="editor-textarea"
                                placeholder="开始编写您的文章...支持Markdown语法"></textarea>
                        </div>
                    </section>

                    <!-- 预览 -->
                    <section class="editor-section">
                        <div class="editor-header">
                            <div class="editor-title">实时预览</div>
                            <div class="editor-tools">
                                <button class="editor-tool" id="togglePreview">全屏预览</button>
                            </div>
                        </div>
                        <div class="editor-content">
                            <div id="preview" class="preview-content"></div>
                        </div>
                    </section>
                </div>

                <!-- 编辑器底部操作栏 -->
                <div class="editor-actions">
                    <div class="status-info">
                        <div class="status-dot" id="statusDot"></div>
                        <span id="statusText">未保存</span>
                        <span id="wordCount">字数: 0</span>
                    </div>
                    <div class="action-buttons">
                        <button class="btn btn-secondary" id="saveDraftBtn">保存草稿</button>
                        <button class="btn btn-secondary" id="previewBtn">预览</button>
                        <button class="btn btn-primary" id="publishBtn">发布文章</button>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <footer>
        <div class="container">
            <p>© 2023 TechBlog. 保留所有权利。</p>
            <p>采用 Github 风格设计 | 由开发者为开发者打造</p>
        </div>
    </footer>

    <script>
        // 主题切换功能
        const themeToggle = document.getElementById('themeToggle');
        const themeIcon = themeToggle.querySelector('i');
        const themeText = themeToggle.querySelector('span');

        // 检查本地存储的主题偏好
        const currentTheme = localStorage.getItem('theme') || 'light';
        if (currentTheme === 'dark') {
            document.body.classList.add('dark-theme');
            themeIcon.classList.remove('fa-moon');
            themeIcon.classList.add('fa-sun');
            themeText.textContent = '浅色模式';
            // 加载深色主题的高亮样式
            loadHighlightTheme('dark');
        } else {
            // 加载亮色主题的高亮样式
            loadHighlightTheme('light');
        }

        // 动态加载高亮主题
        function loadHighlightTheme(theme) {
            // 移除现有的高亮样式
            const existingStyle = document.getElementById('highlight-theme');
            if (existingStyle) {
                existingStyle.remove();
            }

            // 创建新的样式链接
            const link = document.createElement('link');
            link.id = 'highlight-theme';
            link.rel = 'stylesheet';

            if (theme === 'dark') {
                link.href = 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/github-dark.min.css';
            } else {
                link.href = 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/github.min.css';
            }

            document.head.appendChild(link);
        }

        themeToggle.addEventListener('click', () => {
            document.body.classList.toggle('dark-theme');

            if (document.body.classList.contains('dark-theme')) {
                themeIcon.classList.remove('fa-moon');
                themeIcon.classList.add('fa-sun');
                themeText.textContent = '浅色模式';
                localStorage.setItem('theme', 'dark');
                loadHighlightTheme('dark');
            } else {
                themeIcon.classList.remove('fa-sun');
                themeIcon.classList.add('fa-moon');
                themeText.textContent = '深色模式';
                localStorage.setItem('theme', 'light');
                loadHighlightTheme('light');
            }

            // 重新高亮代码块
            setTimeout(() => {
                const preview = document.getElementById('preview');
                if (preview) {
                    preview.querySelectorAll('pre code').forEach((block) => {
                        hljs.highlightElement(block);
                    });
                }
            }, 100);
        });

        // 移动端菜单切换
        const mobileMenu = document.getElementById('mobileMenu');
        const navLinks = document.querySelector('.nav-links');
        const menuIcon = mobileMenu.querySelector('i');

        mobileMenu.addEventListener('click', () => {
            navLinks.classList.toggle('active');

            if (navLinks.classList.contains('active')) {
                menuIcon.classList.remove('fa-bars');
                menuIcon.classList.add('fa-times');
            } else {
                menuIcon.classList.remove('fa-times');
                menuIcon.classList.add('fa-bars');
            }
        });

        // 点击菜单外区域关闭菜单
        document.addEventListener('click', (e) => {
            if (!e.target.closest('.navbar') && navLinks.classList.contains('active')) {
                navLinks.classList.remove('active');
                menuIcon.classList.remove('fa-times');
                menuIcon.classList.add('fa-bars');
            }
        });

        // 窗口大小变化时调整菜单状态
        window.addEventListener('resize', () => {
            if (window.innerWidth > 768) {
                navLinks.classList.remove('active');
                menuIcon.classList.remove('fa-times');
                menuIcon.classList.add('fa-bars');
            }
        });

        // 博客发布页面功能
        document.addEventListener('DOMContentLoaded', function () {
            // 初始化Markdown解析器
            marked.setOptions({
                highlight: function (code, lang) {
                    if (lang && hljs.getLanguage(lang)) {
                        return hljs.highlight(code, { language: lang }).value;
                    }
                    return hljs.highlightAuto(code).value;
                },
                breaks: true,
                gfm: true
            });

            // 获取DOM元素
            const markdownEditor = document.getElementById('markdownEditor');
            const preview = document.getElementById('preview');
            const statusDot = document.getElementById('statusDot');
            const statusText = document.getElementById('statusText');
            const wordCount = document.getElementById('wordCount');
            const saveDraftBtn = document.getElementById('saveDraftBtn');
            const previewBtn = document.getElementById('previewBtn');
            const publishBtn = document.getElementById('publishBtn');
            const tagInput = document.getElementById('tagInput');
            const tagsContainer = document.getElementById('tagsContainer');
            const togglePreviewBtn = document.getElementById('togglePreview');
            const editorTools = document.querySelectorAll('.editor-tool[data-insert]');

            // 标签管理
            let tags = [];

            // 从本地存储加载草稿
            function loadDraft() {
                const draft = localStorage.getItem('techblog_draft');
                if (draft) {
                    const data = JSON.parse(draft);
                    document.getElementById('articleTitle').value = data.title || '';
                    document.getElementById('articleExcerpt').value = data.excerpt || '';
                    document.getElementById('articleCategory').value = data.category || '';
                    document.getElementById('articleCover').value = data.cover || '';
                    markdownEditor.value = data.content || '';

                    // 加载标签
                    tags = data.tags || [];
                    renderTags();

                    updatePreview();
                    updateWordCount();
                    setStatus('saved');
                }
            }

            // 保存草稿到本地存储
            function saveDraft() {
                const draft = {
                    title: document.getElementById('articleTitle').value,
                    excerpt: document.getElementById('articleExcerpt').value,
                    category: document.getElementById('articleCategory').value,
                    cover: document.getElementById('articleCover').value,
                    content: markdownEditor.value,
                    tags: tags,
                    timestamp: new Date().toISOString()
                };

                localStorage.setItem('techblog_draft', JSON.stringify(draft));
                setStatus('saved');
            }

            // 设置状态指示器
            function setStatus(status) {
                if (status === 'saved') {
                    statusDot.classList.add('saved');
                    statusText.textContent = '已保存';
                } else {
                    statusDot.classList.remove('saved');
                    statusText.textContent = '未保存';
                }
            }

            // 更新预览
            function updatePreview() {
                const markdownText = markdownEditor.value;
                preview.innerHTML = marked.parse(markdownText);

                // 在预览更新后重新高亮代码
                preview.querySelectorAll('pre code').forEach((block) => {
                    hljs.highlightElement(block);
                });
            }

            // 更新字数统计
            function updateWordCount() {
                const text = markdownEditor.value;
                // 简单的字数统计（中文字符算一个，英文单词算一个）
                const chineseChars = text.match(/[\u4e00-\u9fa5]/g) || [];
                const englishWords = text.match(/\b\w+\b/g) || [];
                const count = chineseChars.length + englishWords.length;
                wordCount.textContent = `字数: ${count}`;
            }

            // 渲染标签
            function renderTags() {
                // 清除现有标签（除了输入框）
                const existingTags = tagsContainer.querySelectorAll('.tag');
                existingTags.forEach(tag => tag.remove());

                // 添加标签
                tags.forEach(tag => {
                    const tagElement = document.createElement('span');
                    tagElement.className = 'tag';
                    tagElement.innerHTML = `
                        ${tag}
                        <span class="tag-remove" data-tag="${tag}">×</span>
                    `;
                    tagsContainer.insertBefore(tagElement, tagInput);
                });

                // 添加标签删除事件
                tagsContainer.querySelectorAll('.tag-remove').forEach(removeBtn => {
                    removeBtn.addEventListener('click', function () {
                        const tagToRemove = this.getAttribute('data-tag');
                        tags = tags.filter(tag => tag !== tagToRemove);
                        renderTags();
                        setStatus('unsaved');
                    });
                });
            }

            // 添加标签
            function addTag(tagText) {
                const trimmedTag = tagText.trim();
                if (trimmedTag && !tags.includes(trimmedTag)) {
                    tags.push(trimmedTag);
                    renderTags();
                    setStatus('unsaved');
                }
            }

            // 重置表单函数
            function resetForm() {
                document.getElementById('articleTitle').value = '';
                document.getElementById('articleExcerpt').value = '';
                document.getElementById('articleCategory').value = '';
                document.getElementById('articleCover').value = '';
                markdownEditor.value = '';
                tags = [];
                renderTags();
                updatePreview();
                updateWordCount();
            }

            // 事件监听器
            markdownEditor.addEventListener('input', function () {
                updatePreview();
                updateWordCount();
                setStatus('unsaved');
            });

            // 自动保存草稿（防抖）
            let saveTimeout;
            markdownEditor.addEventListener('input', function () {
                clearTimeout(saveTimeout);
                saveTimeout = setTimeout(saveDraft, 2000);
            });

            // 表单字段变化时标记为未保存
            document.querySelectorAll('.form-input, .form-select, .form-textarea').forEach(input => {
                input.addEventListener('input', () => setStatus('unsaved'));
            });

            // 保存草稿按钮
            saveDraftBtn.addEventListener('click', saveDraft);

            // 预览按钮
            previewBtn.addEventListener('click', function () {
                alert('在实际应用中，这里会打开一个新窗口或模态框显示完整预览。');
            });

            // 发布按钮
            publishBtn.addEventListener('click', function () {
                const title = document.getElementById('articleTitle').value.trim();
                const content = markdownEditor.value.trim();

                if (!title) {
                    alert('请输入文章标题');
                    return;
                }

                if (!content) {
                    alert('请输入文章内容');
                    return;
                }

                // 模拟发布过程
                publishBtn.disabled = true;
                publishBtn.textContent = '发布中...';

                setTimeout(() => {
                    alert('文章发布成功！在实际应用中，这里会将文章提交到服务器。');
                    // 清除草稿
                    localStorage.removeItem('techblog_draft');
                    // 重置表单
                    resetForm();
                    setStatus('saved');

                    publishBtn.disabled = false;
                    publishBtn.textContent = '发布文章';
                }, 1500);
            });

            // 标签输入
            tagInput.addEventListener('keydown', function (e) {
                if (e.key === 'Enter') {
                    e.preventDefault();
                    addTag(this.value);
                    this.value = '';
                }

                if (e.key === 'Backspace' && this.value === '' && tags.length > 0) {
                    tags.pop();
                    renderTags();
                    setStatus('unsaved');
                }
            });

            // 编辑器工具按钮
            editorTools.forEach(tool => {
                tool.addEventListener('click', function () {
                    const textToInsert = this.getAttribute('data-insert');
                    const start = markdownEditor.selectionStart;
                    const end = markdownEditor.selectionEnd;
                    const selectedText = markdownEditor.value.substring(start, end);

                    // 插入文本
                    markdownEditor.value = markdownEditor.value.substring(0, start) +
                        textToInsert +
                        markdownEditor.value.substring(end);

                    // 设置光标位置
                    markdownEditor.focus();
                    markdownEditor.setSelectionRange(start + textToInsert.length, start + textToInsert.length);

                    // 更新预览和状态
                    updatePreview();
                    updateWordCount();
                    setStatus('unsaved');
                });
            });

            // 全屏预览切换 - 修复2：修复全屏预览功能
            let isFullscreen = false;
            togglePreviewBtn.addEventListener('click', function () {
                const previewSection = document.querySelector('.editor-section:last-child');
                const editorSection = document.querySelector('.editor-section:first-child');
                const editorLayout = document.querySelector('.editor-layout');

                if (isFullscreen) {
                    // 退出全屏
                    previewSection.style.flex = '1';
                    editorSection.style.display = 'flex';
                    this.textContent = '全屏预览';
                    editorLayout.style.height = '600px';
                } else {
                    // 进入全屏
                    previewSection.style.flex = '2';
                    editorSection.style.display = 'none';
                    this.textContent = '退出全屏';
                    editorLayout.style.height = '800px'; // 增加高度以获得更好的全屏体验
                }

                isFullscreen = !isFullscreen;
            });

            // 初始化
            loadDraft();
            updatePreview();
            updateWordCount();
        });
    </script>
</body>

</html>